package com.example.administrator.printoutindemo.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.text.TextPaint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.dds.soap.SoapListener;
import com.dds.soap.SoapParams;
import com.dds.soap.SoapUtil;
import com.example.administrator.printoutindemo.R;
import com.example.administrator.printoutindemo.bean.LocationBean;
import com.example.administrator.printoutindemo.http.SPUtil;
import com.jess.arms.base.ArmsBaseConstants;
import com.jess.arms.base.BaseConstants;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.SharedPreferencesHelper;
import com.xiaoxing.gifloadingview.LoadingDialogUtil;

import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;

import java.util.List;

import me.jessyan.armscomponent.commonsdk.utils.ButtonUtils;

/**
 * Created by Administrator on 2018/9/11.
 */

public class LocationAdapter extends BaseAdapter {

    Context context;
    List<LocationBean> mlist;
    LayoutInflater inflater;
    private ChuKuSuccess mChuKuSuccess;
    private SharedPreferencesHelper mSharedPreferencesHelper;

    public LocationAdapter(Context context, List<LocationBean> mlist) {
        this.context = context;
        this.mlist = mlist;
        this.inflater = LayoutInflater.from(context);
    }


    public LocationAdapter(Context context, List<LocationBean> mlist, ChuKuSuccess chuKuSuccess, SharedPreferencesHelper sharedPreferencesHelper) {
        this.context = context;
        this.mlist = mlist;
        this.mChuKuSuccess = chuKuSuccess;
        this.inflater = LayoutInflater.from(context);
        this.mSharedPreferencesHelper = sharedPreferencesHelper;
    }

    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public Object getItem(int i) {
        return mlist.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        Viewholder holder = null;
        if (view == null) {
            view = getInflater().inflate(R.layout.item_location, null);
            holder = new Viewholder();
            holder.tv_ckss = view.findViewById(R.id.tv_ckss);
            holder.tv_spname = view.findViewById(R.id.tv_spname);
            holder.tv_nums = view.findViewById(R.id.tv_nums);
            holder.tv_material = view.findViewById(R.id.tv_material);
            holder.tv_sptname = view.findViewById(R.id.tv_sptname);
            holder.tv_wlmc = view.findViewById(R.id.tv_wlmc);
            holder.tv_ckmc = view.findViewById(R.id.tv_ckmc);
            holder.tv_cwmc = view.findViewById(R.id.tv_cwmc);
            view.setTag(holder);
        } else {
            holder = (Viewholder) view.getTag();
        }
        if (mlist.get(i).getMaterial() != null) {
            holder.tv_material.setText(mlist.get(i).getOrderNo());
        }
        if (mlist.get(i).getNums() != null) {
            holder.tv_nums.setText(mlist.get(i).getNums());
        }
        if (mlist.get(i).getSpname() != null) {
            holder.tv_spname.setText(mlist.get(i).getProjectname());
        }
        if (mlist.get(i).getSptname() != null) {
            holder.tv_sptname.setText(mlist.get(i).getInvnums());
        }
        if (mlist.get(i).getMaterial() != null) {
            holder.tv_wlmc.setText(mlist.get(i).getMaterial());
        }
        if (mlist.get(i).getSpname() != null) {
            holder.tv_cwmc.setText(mlist.get(i).getSpname());
        }
        if (mlist.get(i).getStorename() != null) {
            holder.tv_ckmc.setText(mlist.get(i).getStorename());
        }
        holder.tv_ckss.setText("出库");

        if (mlist.get(i).getIsScan().equals("1")) {
            holder.tv_ckss.setTextColor(Color.parseColor("#751D1C"));
            holder.tv_ckss.setBackground(context.getResources().getDrawable(R.drawable.public_shape_red_border_corner_5dp));
            TextPaint paint = holder.tv_ckss.getPaint();
            paint.setFakeBoldText(true);
        } else {
            holder.tv_ckss.setTextColor(context.getResources().getColor(R.color.public_title_bg));
            holder.tv_ckss.setBackground(context.getResources().getDrawable(R.drawable.public_shape_blue_border_corner_5dp));
            TextPaint paint = holder.tv_ckss.getPaint();
            paint.setFakeBoldText(false);
        }

        final Viewholder finalHolder = holder;
        holder.tv_ckss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!ButtonUtils.isFastDoubleClick()) {

                } else {
                    ArmsUtils.makeText(context, "10s内请勿重复点击");
                    return;
                }

                if (!mlist.get(i).getIsScan().equals("1")) {
                    ArmsUtils.makeText(context, "请先扫描二维码");
                    return;
                }

                String m = SPUtil.getString(context, "newmidss", "");
                SoapParams params = new SoapParams();

                params.put("uid", SPUtil.getString(context, "iduser", ""));
                params.put("mid", m);
                params.put("out_storageList_id", mlist.get(i).getOut_storageList_id());
                params.put("spid", mlist.get(i).getSpid());
                Log.e("map============", String.valueOf(params));
                outStoreForAndroid(params, finalHolder);

            }
        });

        return view;
    }

    public LayoutInflater getInflater() {
        return inflater;
    }

    private void outStoreForAndroid(SoapParams params, Viewholder finalHolder) {


        String methodName = "OutStoreForAndroid";


        /**
         * 请求
         */
        SoapUtil.getInstance(context).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                LoadingDialogUtil.dismissDialog();

                String result = object.getProperty(methodName + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    Log.e("789456========", result);
                    SPUtil.putString(context, "newmidss", "");
                    if (result.equals("true")) {
                        finalHolder.tv_ckss.setText("出库完成");
                        mChuKuSuccess.refreshData();
                    } else {
                        Toast.makeText(context, "出库失败", Toast.LENGTH_SHORT).show();
                    }
                } else {

                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                LoadingDialogUtil.dismissDialog();
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                LoadingDialogUtil.dismissDialog();
            }
        }, getAapiUserName(), getApiPwd());
    }

    @NonNull
    protected String getUrl() {
        return "http://" + mSharedPreferencesHelper.getString(ArmsBaseConstants.IP) + ":"
                + mSharedPreferencesHelper.getString(ArmsBaseConstants.PORT)
                + "/StoreService.asmx";
    }

    protected String getAapiUserName() {
        return mSharedPreferencesHelper.getString(BaseConstants.API_USER_NAME);
    }

    protected String getApiPwd() {
        return mSharedPreferencesHelper.getString(BaseConstants.API_PWD);
    }

    public void setInflater(LayoutInflater inflater) {
        this.inflater = inflater;
    }

    public void addAll(List<LocationBean> lcb) {
        mlist.clear();
        mlist.addAll(lcb);
        notifyDataSetChanged();
    }

    public interface ChuKuSuccess {
        void refreshData();
    }

    class Viewholder {
        TextView tv_ckss, tv_spname, tv_nums, tv_material, tv_sptname, tv_wlmc, tv_ckmc, tv_cwmc;
    }
}
